package com.xinsoft.controller.wms;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmStockMoveDTO;
import com.xinsoft.entity.dto.ScmStockMoveParam;
import com.xinsoft.entity.po.ScmStockMove;
import com.xinsoft.entity.po.ScmStockMoveItem;
import com.xinsoft.entity.vo.ScmProduceReturnPageVo;
import com.xinsoft.entity.vo.ScmStockMovePageVo;
import com.xinsoft.entity.vo.ScmStockMoveVo;
import com.xinsoft.service.ScmStockMoveItemService;
import com.xinsoft.service.ScmStockMoveService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 库存调拨
 */
@RestController
@RequestMapping("/scm/stockMove")
public class ScmStockMoveController {
    @Autowired
    private ScmStockMoveService scmStockMoveService;
    @Autowired
    private ScmStockMoveItemService stockMoveItemService;
    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 保存
     */
    @PostMapping("/saveOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> saveOrUpdate(@RequestBody ScmStockMoveDTO dto){
        ScmStockMove scmStockMove = new ScmStockMove();
        BeanUtils.copyProperties(dto,scmStockMove);
        scmStockMove.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        if (ObjectUtil.isNotEmpty(dto.getId())){
            stockMoveItemService.remove(new LambdaQueryWrapper<ScmStockMoveItem>().eq(ScmStockMoveItem::getMoveId,dto.getId()));
        }else {
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.STOCK_MOVE_DOCUMENT);
        }
        scmStockMoveService.saveOrUpdate(scmStockMove);
        List<ScmStockMoveItem> itemList = dto.getItemList();
        itemList.forEach(item -> item.setMoveId(scmStockMove.getId()));
        stockMoveItemService.saveBatch(itemList);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> audit(@ApiParam(value = "id",required = true) @RequestParam("id") Integer id){
        return Response.succeed(scmStockMoveService.audit(id));
    }

    /**
     * 反审核
     */
    @GetMapping("/reverseAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> reverseAudit(@ApiParam(value = "id",required = true) @RequestParam("id") Integer id){
        return Response.succeed(scmStockMoveService.reverseAudit(id));
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmStockMoveParam param) {
        return scmStockMoveService.findPageListByParam(param);
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmStockMoveVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(scmStockMoveService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        List<ScmStockMove> scmStockMoves = scmStockMoveService.listByIds(ids);
        scmStockMoves.forEach(move->{
            if (Objects.equals(move.getAuditStatus(),Const.AUDIT_STATUS_NORMAL)){
                throw new BDException("已审核的数据不能删除");
            }
        });
        scmStockMoveService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmStockMoveParam param, HttpServletResponse response) throws Exception {
        List<ScmStockMovePageVo> list = scmStockMoveService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmStockMovePageVo.class, "库存调拨", "库存调拨", response, "库存调拨");
        return Response.succeed();
    }
}
